/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gammapeit.customjaas;

import com.gammapeit.customjaas.database.Grupo;
import com.gammapeit.customjaas.database.Usuario;
import com.gammapeit.customjaas.database.UsuarioJpaController;
import com.sun.appserv.security.AppservRealm;
import com.sun.enterprise.security.auth.realm.BadRealmException;
import com.sun.enterprise.security.auth.realm.InvalidOperationException;
import com.sun.enterprise.security.auth.realm.NoSuchRealmException;
import com.sun.enterprise.security.auth.realm.NoSuchUserException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

/**
 *
 * @author Mike
 */
public class CustomRealm extends AppservRealm{
    private static final String JAAS_CONTEXT="jaas-context";
 
   public CustomRealm() {
   }
 
   /**
    * Initialize a realm with some properties. This can be used
    * when instantiating realms from their descriptions. This
    * method may only be called a single time.
    *
    * @param properties - Key-value pairs defined in the Console's Realm declaration.
    *
    * @exception BadRealmException if the configuration parameters identify a corrupt realm
    * @exception NoSuchRealmException if the configuration parameters specify a realm which doesn't exist
    */
   @Override
   public void init(Properties properties) throws BadRealmException, NoSuchRealmException {
 
       System.out.println("Init MyRealm");
 
       // Pass the properties declared in the console to the system
       String propJaasContext=properties.getProperty(JAAS_CONTEXT);
       if (propJaasContext!=null) {
          setProperty(JAAS_CONTEXT, propJaasContext);
       }
   }
 
   /**
    * Returns a short (preferably less than fifteen characters) description
    * of the kind of authentication which is supported by this realm.
    */
   @Override
   public String getAuthType() {
      return "JDBC/LDAP";
   }
 
   /**
    * Returns the name of all the groups that this user belongs to.
    *
    * @param username name of the user in this realm whose group listing is needed.
    *
    * @return enumeration of group names (strings).
    *
    * @exception InvalidOperationException thrown if the realm does not
    * support this operation - e.g. Certificate realm does not support this
    * operation
    */
   @Override
   public Enumeration getGroupNames(String user) throws InvalidOperationException, NoSuchUserException {
 
      System.out.println("getGroupNames("+user+")");
 
      Vector<String> vector = new Vector<String>();
 
      // Get the groups from your DB
       UsuarioJpaController control = new UsuarioJpaController();
       Usuario usr = control.findUsuarioByUsername(user);
      
       System.out.println("+++++++++++++++++++++  Usuario = " + usr);
       
       for (Grupo temp : usr.getGrupoList()) {
           vector.add(temp.getNombre());
       }
 
      return vector.elements();
   }
}
